package jpb.module.rest.manager;

import jpb.constant.Reply;
import jpb.constant.ReturnState;
import jpb.constant.StatusCode;
import jpb.module.daomain.manager.Manager;
import jpb.module.service.manager.ManagerService;
import jpb.security.HandleCookie;
import jpb.security.HandleToken;
import jpb.util.MD5Coder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by haol on 2017/3/17.
 */
@RestController
@RequestMapping("/api/web/manager")
public class ManagerController {
        @Autowired
        private ManagerService managerService;

        private HandleToken handleToken = new HandleToken();

        /**
         * @api {POST} /api/web/manager/register 注册
         * @apiGroup Manager
         * @apiName Register
         * @apiDescription 管理员注册账户信息
         * @apiParam {String} account 账号(not null)
         * @apiParam {String} password 密码(not null)
         * @apiParam {String} mobile 联系电话(not null)
         * @apiParam {String} remark 备注
         * @apiParamExample {json} 请求样例：
         * POST /api/web/manager/register
         * {
         * "password":"test"
         * "account":"test"
         * "mobile":"17090077783"
         * "remark":"no message"
         * }
         * @apiSuccessExample {json} 返回样例:
         * {
         * "code":0,"msg":"注册成功"
         * }
         */

        @RequestMapping(value = "/register", method = RequestMethod.POST)
        public String register(Manager manager) {
                if (managerService.getByAccount(manager.getAccount()) == null){
                        managerService.add(manager);
                        return new Reply(1,ReturnState.INSERT_SUCCESS).toJson();
                }else {
                        return new Reply(0,ReturnState.NO_UNIQUE).toJson();
                }
        }

        /**
         * @api {PUT} /api/web/manager/modify-pwd 更新密码
         * @apiGroup Manager
         * @apiName modify-pwd
         * @apiDescription 管理员更新密码
         * @apiParam {String} oldPwd 旧密码
         * @apiParam {String} newPwd 新密码
         * @apiParamExample {json} 请求样例：
         * PUT /api/web/manager/modify-pwd
         * {
         *      "oldPwd":"test","newPwd":"newTest"
         * }
         * @apiSuccessExample {json} 返回样例:
         * {
         *      "code":0,"msg":"更改成功"
         * }
         */
        @RequestMapping(value = "/modify-pwd", method = RequestMethod.PUT)
        public String modifyPwd(String oldPwd, String newPwd,
                                HttpServletResponse response, HttpServletRequest request) {
                String token = HandleCookie.getValueByName(request, StatusCode.COOKIE_JWT_WEB);
                String id = handleToken.getValueByKey(token, "id");
                boolean success = managerService.modifyPwd(id, oldPwd, newPwd);
                if (success) {
                        HandleCookie.deleteCookie(response, StatusCode.COOKIE_JWT_WEB);
                        return new Reply(0,ReturnState.UPDATE_SUCCESS).toJson();
                } else {
                        return new Reply(1,ReturnState.FAIL).toJson();
                }
        }

        /**
         * @api {PUT} /api/web/manager/update 更新信息
         * @apiGroup Manager
         * @apiName update
         * @apiDescription 管理员更新信息
         * @apiParam {String} mobile 联系电话
         * @apiParamExample {json} 请求样例：
         * PUT /api/web/manager/update
         * {
         *      "mobile":"18888888888",
         *      "remark":"this is remark"
         * }
         * @apiSuccessExample {json} 返回样例:
         * {
         *      "code":0,"msg":"更改成功"
         * }
         */

        @RequestMapping(value = "/update", method = RequestMethod.PUT)
        public String update(Manager manager,HttpServletRequest request) {

                String token = HandleCookie.getValueByName(request, StatusCode.COOKIE_JWT_WEB);
                String id = handleToken.getValueByKey(token, "id");
                manager.setId(id);

                managerService.update(manager);
                return new Reply(0,ReturnState.UPDATE_SUCCESS).toJson();
        }

        /**
         * @api {GET} /api/web/manager/see 获取登录管理员信息
         * @apiGroup Manager
         * @apiName see
         * @apiDescription 获取当前登录的管理员的个人信息
         * @apiParamExample {json} 请求样例：
         * {GET} /api/web/manager/see
         * @apiSuccessExample {json} 返回样例:
         * {
         *      "code":0,"msg":"获取成功","data":{"id":"f3b66fb8-95de-4d1e-9380-12f8f5b9a4e9","account":"test","password":"889255f1c9c8f12a353be255f78a848b","createTime":"Tue Apr 18 22:00:43 CST 2017","LoginTime":"Apr 26, 2017 6:20:48 PM","mobile":"17090077783"}
         * }
         */
        @RequestMapping(value = "/see", method = RequestMethod.GET)
        public String see(HttpServletRequest request) {
                String token = HandleCookie.getValueByName(request, StatusCode.COOKIE_JWT_WEB);
                String id = handleToken.getValueByKey(token, "id");

                Manager manager = managerService.get(id);
                Reply<Manager> reply = new Reply<Manager>(0,"获取成功",manager);
                return reply.toJson();
        }

        /**
         * @api {POST} /api/web/manager/i/{oid}/org 增加管理员
         * @apiGroup ManagerOrg
         * @apiName addOrgMan
         * @apiDescription 给id为{oid}的组织增加管理员
         * @apiParam {String} account 账号
         * @apiParamExample {json} 请求样例：
         * {POST} /api/web/manager/i/organize_id/org
         * {
         *      "account":"test"
         * }
         * @apiSuccessExample {json} 返回样例:
         * {
         *      "code":0,"msg":"增加成功"
         * }
         */

        @RequestMapping(value = "/i/{oid}/org", method = RequestMethod.POST)
        public String addOrgMan(String account, @PathVariable String oid) {
                Manager manager = managerService.getByAccount(account);
                if (manager == null) {
                       return new Reply(1,ReturnState.NO_FIND).toJson();
                } else {
                        String mid = manager.getId();
                        managerService.addOrgMan(mid, oid,0);
                        return new Reply(1,ReturnState.INSERT_SUCCESS).toJson();
                }
        }

        /**
         * @api {GET} /api/web/manager/{account} 获取管理员信息ByAccount
         * @apiGroup Manager
         * @apiName getByAccount
         * @apiParam {String} account 账号
         * @apiParamExample {json} 请求样例：
         * {GET} /api/web/manager/test
         * @apiSuccessExample {json} 返回样例:
         * {
         *       "code":0,"msg":"获取成功","data":{"id":"f3b66fb8-95de-4d1e-9380-12f8f5b9a4e9","account":"test","password":"889255f1c9c8f12a353be255f78a848b","createTime":"Tue Apr 18 22:00:43 CST 2017","LoginTime":"Apr 26, 2017 6:20:48 PM","mobile":"17090077783"}
         * }
         */
        @RequestMapping(value = "/{account}", method = RequestMethod.GET)
        public String getByAccount(@PathVariable String account) {
                Manager manager = managerService.getByAccount(account);
                if (manager == null) {
                        return new Reply(1,ReturnState.NO_FIND).toJson();
                } else {
                        manager.setPassword(null);
                        return new Reply<Manager>(0,ReturnState.INSERT_SUCCESS,manager).toJson();
                }
        }
        /**
         * @api {DELETE} /api/web/manager/i/{oid}/org 删除管理员
         * @apiGroup ManagerOrg
         * @apiName delOrgMan
         * @apiDescription 删除id为{oid}的组织管理员
         * @apiParam {String} id 管理员Id
         * @apiParamExample {json} 请求样例：
         * {DELETE} /api/web/manager/i/organize_id/org
         * {
         *      "id":"11"
         * }
         * @apiSuccessExample {json} 返回样例:
         * {
         *      "code":0,"msg":"删除成功"
         * }
         */
        @RequestMapping(value = "/i/{oid}/org", method = RequestMethod.DELETE)
        public String delOrgMan(String mid, @PathVariable String oid) {
                if (managerService.checkManager(oid,mid)) {
                        return new Reply(1,ReturnState.NO_FIND).toJson();
                } else {
                        managerService.deleteOrgMan(mid, oid);
                        return new Reply(0,ReturnState.DELETE_SUCCESS).toJson();
                }
        }

        /**
         * @api {GET} /api/web/manager/i/{oid}/org 获取全部管理员信息
         * @apiGroup ManagerOrg
         * @apiName get
         * @apiDescription 获取id为{oid}的组织中的全部管理员信息
         * @apiParamExample {json} 请求样例：
         * GET /api/web/manager/i/organize_id/org
         * @apiSuccessExample {json} 返回样例:
         * {
         *      "code":0,"msg":"获取成功","date":[{"id":"f3b66fb8-95de-4d1e-9380-12f8f5b9a4e9","account":"test","password":"889255f1c9c8f12a353be255f78a848b","createTime":"Tue Apr 18 22:00:43 CST 2017","LoginTime":"Apr 26, 2017 6:20:48 PM","mobile":"17090077783"}]
         * }
         */
        @RequestMapping(value = "/i/{oid}/org", method = RequestMethod.GET)
        public String get(@PathVariable String oid) {
                List<Manager> managers = managerService.getByOid(oid);
                Reply<List<Manager>> reply = new Reply<List<Manager>>(0,ReturnState.SUCCESS,managers);
                return reply.toJson();
        }

        /**
         * @api {GET} /api/web/manager/i/{oid}/org-founder  获取创建者
         * @apiGroup ManagerOrg
         * @apiName getFounder
         * @apiDescription 获取id为{oid}的组织创建者
         * @apiParamExample {json} 请求样例：
         * GET /api/web/manager/i/organize_id/org-founder
         * @apiSuccessExample {json} 返回样例:
         * {
         *              "code":0,"msg":"获取成功","date":{"id":"f3b66fb8-95de-4d1e-9380-12f8f5b9a4e9","account":"test","password":"889255f1c9c8f12a353be255f78a848b"}
         * }
         */
        @RequestMapping(value = "/i/{oid}/org-founder", method = RequestMethod.GET)
        public String getFounder(@PathVariable String oid) {
                Manager manager = managerService.getFounder(oid);

                Reply<Manager> reply = new Reply<Manager>(0,ReturnState.SUCCESS,manager);
                return reply.toJson();
        }

        /**
         * @api {GET} /api/web/manager/check-exist  检测账号是否存在
         * @apiGroup Manager
         * @apiName check
         *  @apiParam {String} account 管理员账号
         * @apiDescription 检测管理员账号是否存在,用于管理员注册前的检测
         * @apiParamExample {json} 请求样例：
         * GET /api/web/manager/i/organize_id/check-exist
         * { "account":"test" }
         * @apiSuccessExample {json} 返回样例:
         * { "true"}
         */
        @RequestMapping(value = "/check-exist", method = RequestMethod.GET)
        public boolean checkExist(String account) {
                Manager manager = managerService.getByAccount(account);
                return manager != null;
        }

        /**
         * @api {GET} /api/web/manager/check-self  检验是否为本人
         * @apiGroup Manager
         * @apiName checkSelf
         *  @apiParam {String} password 密码
         * @apiDescription 检验是否为本人，用于对一些敏感操作验证比如删除组织
         * @apiParamExample {json} 请求样例：
         * GET /api/web/manager/check-self
         * {
         *      "password":"test"
         * }
         * @apiSuccessExample {json} 返回样例:
         * {
         *      "true"
         * }
         */
        @RequestMapping(value = "/check-self", method = RequestMethod.GET)
        public boolean checkSelf(HttpServletRequest request, String password) {
                String token = HandleCookie.getValueByName(request, StatusCode.COOKIE_JWT_WEB);
                String mid = handleToken.getValueByKey(token, "id");
                Manager manager = managerService.get(mid);
                String MD5 = MD5Coder.encodeAccountPwd(manager.getAccount(),password);
                if (manager.getPassword().equals(MD5)) {
                        return true;
                } else {
                        return false;
                }
        }
        /**
         * @api {GET} /api/web/manager/check-founder  检测是否为创始人
         * @apiGroup Manager
         * @apiName checkIsFounder
         * @apiParam {String} oid 组织id
         * @apiDescription  检测是否为创始人
         * @apiParamExample {json} 请求样例：
         * GET /api/web/manager/check-founder
         * {
         *         "oid":"organize-id"
         * }
         * @apiSuccessExample {json} 返回样例:
         * {
         *      "true"
         * }
         */
        @RequestMapping(value = "/check-founder", method = RequestMethod.GET)
        public boolean checkIsFounder(HttpServletRequest request,String oid) {
                String token = HandleCookie.getValueByName(request, StatusCode.COOKIE_JWT_WEB);
                String mid = handleToken.getValueByKey(token, "id");
                return managerService.checkFounder(mid,oid);
        }


        @RequestMapping(value = "/{id}/update-head")
        public String updateHead(Manager manager) {
                managerService.updateHead(manager);
                return ReturnState.UPDATE_SUCCESS;
        }
}
